package com.hilaver.dzmis.web.servlet.procurement;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Locale;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.procurement.ProductQuality;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.service.impl.ProductQualityServiceImpl;
import com.hilaver.dzmis.web.servlet.AbstractBaseServlet;

public class ProductQualityServlet extends AbstractBaseServlet{
	
	private static final long serialVersionUID = 1L;
	
	private ProductQualityServiceImpl pqs;
	
	private ProductQuality pq;

	public ProductQualityServlet(){
		this.fullName = ProductQuality.class.getName();
		this.simpleName = ProductQuality.class.getSimpleName();
		this.pqs = new ProductQualityServiceImpl();
	}
	
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter writer = response.getWriter();
		try {
			this.pqs.setLocale((Locale) request.getSession()
					.getAttribute(Constants.SESSION_LOCALE));
			String outputStr = execute(request);
			if (this.action == ACTION_EDIT) {
				response.setContentType(Constants.HTML_RESPONSE_TYPE);
			}
			writer.write(outputStr);
		} catch (Exception e) {
			writer.print(e.getMessage());
			e.printStackTrace();
		}
		writer.close();
	}

	protected String execute(HttpServletRequest request) throws Exception {
		super.setAction(request);
		String rtn = "";
		switch (this.action) {
			case ACTION_GET: // "get one"
				super.setId(request);
				rtn = this.pqs.get(this.id);
				break;
			case ACTION_GET_WITH_DESCRIPTION:
				
				break;
			case ACTION_DELETE:
				super.setId(request);
				rtn = this.pqs.delete(this.id);
				break;
			case ACTION_GET_ALL_PAGINATION_FILTER:
				super.setPaginationParam(request);
				super.setFilterParam(request);
				rtn = this.pqs.getAllPagination(this.page, this.sort,
						this.dir, super.filters);
				break;
			case ACTION_EDIT:
				this.fillObj(request);
				rtn = this.pqs.edit(this.pq);
				break;
		}
		return rtn;
	}
	
	private void fillObj(HttpServletRequest request) throws Exception{
		super.fillObj(request);
		this.pq = (ProductQuality)this.obj;
		Integer poiId = Integer.parseInt(request.getParameter("poiId"));
		ProductOrderItem poi = new ProductOrderItem();
		poi.setId(poiId);
		this.pq.setProductOrderItem(poi);
	}

}
